package org.xshop.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.xshop.model.SendMsg;
import org.xshop.model.SysMsg;
import org.xshop.service.ISendMsgService;
import org.xshop.service.ISysMsgService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.ibase4j.core.Constants;
import top.ibase4j.core.base.AppBaseController;
import top.ibase4j.core.util.CacheUtil;
import top.ibase4j.core.util.WebUtil;

/**
 * <p>
 * 短信 前端控制器
 * </p>
 *
 * @author ShenHuaJie
 * @since 2017-03-12
 */
@Controller
@RequestMapping("/app/")
@Api(tags = "SystemSms", description = "APP-短信接口")
public class SysMsgController extends AppBaseController<SysMsg, ISysMsgService> {
    @Autowired
    private ISendMsgService sendMsgService;

    @PostMapping("msg.api")
    @ApiOperation(
        value = "发送短信",
        notes = "短信发送在服务器上有次数限制\n" +
                "为了方便测试，当调用此接口的手机号以199开头，则自动得到验证码123456",
                produces = MediaType.APPLICATION_JSON_VALUE)
    public Object update(HttpServletRequest request, SendMsg record) {
        record = WebUtil.getParameter(request, SendMsg.class);
        Assert.notNull(record.getMsgType(), "短信类型不能为空.");
        Assert.notNull(record.getPhone(), "手机号不能为空.");
        Assert.notNull(record.getBizType(), "业务类型不能为空.");

        if (record.getPhone().startsWith("199")) {
            CacheUtil.getCache().set(Constants.MSGCHKTYPE.REGISTER + record.getPhone(), "123456", 120);
            CacheUtil.getCache().set(Constants.MSGCHKTYPE.LOGIN + record.getPhone(), "123456", 120);
        } else {
            sendMsgService.sendMsg(record);
        }

        ModelMap modelMap = new ModelMap();
        return setSuccessModelMap(modelMap);
    }
}
